﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using BLL;
namespace Stationery.UC
{
    public partial class UC_Product_Update : System.Web.UI.UserControl
    {
        Product product = new Product();
        BLL_Product bll_Product = new BLL_Product();
        BLL_MenuURL bll_MenuURL = new BLL_MenuURL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ASPxComboBoxCategory.DataSource = bll_MenuURL.Get_MenuURL_Dynamic(String.Format("TYPE='{0}' AND ISNULL(ID_PARENT,0)<>0", MenuURL.column));
                ASPxComboBoxCategory.DataBind();
                if (Request.Params["status"] == "edit")
                {
                    Int64 intID = Convert.ToInt64(Request.Params["id_product"].ToString());
                    product = bll_Product.Lst_Product(bll_Product.Get_Product(intID))[0];
                    ASPxComboBoxCategory.Value = product.ID_MENUURL;
                    ASPxTextBoxCode.Text = product.CODE;
                    ASPxTextBoxName.Text = product.NAME;
                    ASPxMemoDescription.Text = product.DESCRIPTION;
                    ASPxSpinEditPrice.Value = product.PRICE;
                    ASPxTextBoxOrigin.Text = product.ORIGIN;
                    ASPxTextBoxOtherName.Text = product.OTHER_NAME;
                    ASPxTextBoxStorageTime.Text = product.STORAGE_TIME;
                    ASPxSpinEditSale.Value = product.SALE;
                    ASPxComboBoxStatStock.Value = product.STAT_STOCK;
                    ASPxTextBoxURL.Text = product.IMAGE_DIRECTORY;
                    ASPxCheckBoxDisplay.Checked = product.blnDisplay;
                    ASPxSpinEditWeight.Value = product.WEIGHT;
                    ASPxSpinEditSequence.Value = product.SEQUENCE;
                    ASPxComboBoxCategory.DataBind();
                    if (!(product.IMAGE_DIRECTORY.Contains("http") || product.IMAGE_DIRECTORY.Contains("www")))
                    {
                        ASPxCheckBoxURL.Checked = false;
                    }
                }
                else
                {
                    //them moi- tu ghi so thu tu moi
                    //try
                    //{
                    //    ASPxSpinEditSequence.Value = bll_Product.Get_Product_All().Rows.Count;
                    //}
                    //catch
                    //{

                    //}
                }
                ASPxCheckBoxURL_CheckedChanged(null, null);
            }
        }

        protected void ASPxButtonSave_Click(object sender, EventArgs e)
        {
            //luu
            string strStatus = "";
            string strFileName = "";
            Int32 intID;
            if (Request.Params["status"] != null)
            {
                strStatus = Request.Params["status"].ToString();
                if (Request.Params["status"] == "edit")
                {
                    intID = Convert.ToInt32(Request.Params["id_product"].ToString());
                    product.ID = intID;
                }
            }
            product.ID_MENUURL = (ASPxComboBoxCategory.Value == DBNull.Value ? 0 : Convert.ToInt32(ASPxComboBoxCategory.Value));
            product.CODE = ASPxTextBoxCode.Text;
            product.NAME = ASPxTextBoxName.Text;
            product.DESCRIPTION = ASPxMemoDescription.Text;
            product.CREATE_DATE = DateTime.Now;
            product.PRICE = Convert.ToInt32(ASPxSpinEditPrice.Value);
            product.ORIGIN = ASPxTextBoxOrigin.Text;
            product.OTHER_NAME = ASPxTextBoxOtherName.Text;
            product.SALE = Convert.ToDecimal(ASPxSpinEditSale.Value);
            product.STAT_STOCK = Convert.ToString(ASPxComboBoxStatStock.Value);
            product.STORAGE_TIME = ASPxTextBoxStorageTime.Text;
            product.blnDisplay = ASPxCheckBoxDisplay.Checked;
            product.WEIGHT = Convert.ToInt32(ASPxSpinEditWeight.Value);
            product.SEQUENCE = Convert.ToInt32(ASPxSpinEditSequence.Value);
            try
            {
                if (!ASPxCheckBoxURL.Checked)
                {
                    if (ASPxUploadControlImage.HasFile)
                    {
                        if (String.IsNullOrEmpty(product.IMAGE_DIRECTORY))//neu da co file cũ thì xóa đi để giảm dung lượng
                        {
                            string strFile = Server.MapPath(product.IMAGE_DIRECTORY);
                            if (File.Exists(strFile))
                            {
                                File.Delete(strFile);
                            }
                        }
                        string[] extension = ASPxUploadControlImage.FileName.Split('.');
                        strFileName = bll_Product.Get_Product_MaxID().ToString() + String.Format("-{0}", product.ID) + "." + extension[1];
                        product.IMAGE_DIRECTORY = Utils.upload_directory + strFileName;
                        ASPxUploadControlImage.SaveAs(Server.MapPath(product.IMAGE_DIRECTORY), true);
                    }
                    else
                    {
                        product.IMAGE_DIRECTORY = ASPxTextBoxURL.Text;
                    }
                }
                else
                {
                    product.IMAGE_DIRECTORY = ASPxTextBoxURL.Text;
                }
            }
            catch
            {
                Response.Write("<script>alert('Kiểm tra lại File Upload')</script>");
                return;
            }
            if (bll_Product.InsertUpdate_Product(product))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Thành công');location.href = 'Admin.aspx?id=product';", true);
            }
        }

        protected void ASPxButtonCancel_Click(object sender, EventArgs e)
        {
            Session[Utils.SESSION_PRODUCT] = null;
            Response.Redirect(UtilsURL.product);
        }

        protected void ASPxButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.product);
        }

        protected void ASPxCheckBoxURL_CheckedChanged(object sender, EventArgs e)
        {
            ASPxTextBoxURL.Enabled = ASPxCheckBoxURL.Checked;
            id_upload.Visible = !ASPxCheckBoxURL.Checked;
        }
    }
}